package com.bot.adms.dao.impl;

import com.bot.adms.dao.IApplyContentSpScDetailTextDao;
import com.bot.adms.model.entity.ApplyContentSpScDetailText;
import com.bot.adms.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 投放申请文字信息表持久层接口实现
 * @author ：刘威林
 */
public class ApplyContentSpScDetailTextDaoImpl implements IApplyContentSpScDetailTextDao {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;

    /**
     * 添加投放申请文字信息
     * @param applyContentSpScDetailText 投放申请文字信息：包含：投放申请文字信息id,背景图片，显示位置，字体类型，字体大小，字体颜色，文字内容
     * @return Integer 添加成功的数量
     */
    @Override
    public Integer insertApplyContentSpScDetailText(ApplyContentSpScDetailText applyContentSpScDetailText) {
       //获取数据库
        try {
            //1.获取Connection连接
            conn= JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt=conn.createStatement();
            //3.SQL语句
            String sql="INSERT INTO `bo_adms`.`apply_content_sp_sc_detail_text` (`image_url`, `location`, `font_type`, `font_size`, `font_color`, `text_content`, `apply_content_sp_sc_detail_id`) VALUES ('"
                    +applyContentSpScDetailText.getImageUrl()+"', "
                    +applyContentSpScDetailText.getLocation()+", "
                    +applyContentSpScDetailText.getFontType()+", "
                    +applyContentSpScDetailText.getFontSize()+", '"
                    +applyContentSpScDetailText.getFontColor()+"', '"
                    +applyContentSpScDetailText.getTextContent()+"', '"
                    +applyContentSpScDetailText.getApplyContentSpScDetailId()+"');";
            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(conn,stmt);
        }
    }

    /**
     * 修改投放申请文字信息
     * @param applyContentSpScDetailText 投放申请文字信息：包含：投放申请文字信息id,背景图片，显示位置，字体类型，字体大小，字体颜色，文字内容
     * @return Integer 修改成功的数量
     */
    @Override
    public Integer updateApplyContentSpScDetailText(ApplyContentSpScDetailText applyContentSpScDetailText) {

        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="UPDATE `bo_adms`.`apply_content_sp_sc_detail_text` SET `image_url` = '" +applyContentSpScDetailText.getImageUrl()+"', " +
                    "`location` = " +applyContentSpScDetailText.getLocation()+", " +
                    "`font_type` = " +applyContentSpScDetailText.getFontType()+"," +
                    " `font_size` = " +applyContentSpScDetailText.getFontSize()+", " +
                    "`font_color` = '" +applyContentSpScDetailText.getFontColor()+"', " +
                    "`text_content` = '" +applyContentSpScDetailText.getTextContent()+"' " +
                    "WHERE `id` = " +applyContentSpScDetailText.getId()+";";
            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(conn, stmt);
        }
    }

    /**
     * 删除投放申请文字信息
     * @param id 投放申请文字信息id
     * @return Integer 删除成功的数量
     */
    @Override
    public Integer deleteApplyContentSpScDetailText(Long id) {
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="DELETE FROM `bo_adms`.`apply_content_sp_sc_detail_text` WHERE `id` = "
                    +id+";";
            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn,stmt);
        }

    }

    /**
     * 查找（获取）投放申请文字信息
     * @param id 投放申请文字信息id
     * @return  ApplyContentSpScDetailText 投放申请文字信息：包含：投放申请文字信息id,背景图片，显示位置，字体类型，字体大小，字体颜色，文字内容
     */
    @Override
    public ApplyContentSpScDetailText getApplyContentSpScDetailTextById(Long id) {
        ApplyContentSpScDetailText  applyContentSpScDetailText = null;
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="SELECT * FROM `bo_adms`.`apply_content_sp_sc_detail_text` WHERE `id` = " +id+";";
            //4.遍历结果
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                applyContentSpScDetailText = new ApplyContentSpScDetailText();
                applyContentSpScDetailText.setId(rs.getLong("id"));
                applyContentSpScDetailText.setImageUrl(rs.getString("image_url"));
                applyContentSpScDetailText.setLocation(rs.getInt("location"));
                applyContentSpScDetailText.setFontType(rs.getInt("font_type"));
                applyContentSpScDetailText.setFontSize(rs.getInt("font_size"));
                applyContentSpScDetailText.setFontColor(rs.getString("font_color"));
                applyContentSpScDetailText.setTextContent(rs.getString("text_content"));
                applyContentSpScDetailText.setApplyContentSpScDetailId(rs.getLong("apply_content_sp_sc_detail_id"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn,stmt, rs);
        }
        return applyContentSpScDetailText;
    }

    /**
     * 查询投放申请文字信息
     * @param applyContentSpScDetailText 投放申请文字信息：包含：投放申请文字信息id,背景图片，显示位置，字体类型，字体大小，字体颜色，文字内容
     * @return List<ApplyContentSpScDetailText> 投放申请文字信息集合：包含：投放申请文字信息id,背景图片，显示位置，字体类型，字体大小，字体颜色，文字内容
     */
    @Override
    public List<ApplyContentSpScDetailText> searchApplyContentSpScDetailText(ApplyContentSpScDetailText applyContentSpScDetailText) {
        List<ApplyContentSpScDetailText> applyContentSpScDetailTextList = new ArrayList<ApplyContentSpScDetailText>();

        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="SELECT * FROM `bo_adms`.`apply_content_sp_sc_detail_text` WHERE 1=1";
            if(applyContentSpScDetailText.getId()!=null){
                sql+=" AND `id` = " +applyContentSpScDetailText.getId();
            }
            if(applyContentSpScDetailText.getImageUrl()!=null && !applyContentSpScDetailText.getImageUrl().isEmpty()){
                sql+=" AND `image_url` LIKE '%" +applyContentSpScDetailText.getImageUrl()+"%'";
            }
            if(applyContentSpScDetailText.getLocation()!=null){
                sql+=" AND `location` = " +applyContentSpScDetailText.getLocation();
            }
            if(applyContentSpScDetailText.getFontType()!=null){
                sql+=" AND `font_type` = " +applyContentSpScDetailText.getFontType();
            }
            if(applyContentSpScDetailText.getFontSize()!=null){
                sql+=" AND `font_size` = " +applyContentSpScDetailText.getFontSize();
            }
            if(applyContentSpScDetailText.getFontColor()!=null && !applyContentSpScDetailText.getFontColor().isEmpty()){
                sql+=" AND `font_color` LIKE '%" +applyContentSpScDetailText.getFontColor()+"%'";
            }
            if(applyContentSpScDetailText.getTextContent()!=null && !applyContentSpScDetailText.getTextContent().isEmpty()){
                sql+=" AND `text_content` LIKE '%" +applyContentSpScDetailText.getTextContent()+"%'";
            }
            //4.遍历查询结果
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                ApplyContentSpScDetailText applyContentSpScDetailText1 = new ApplyContentSpScDetailText();
                applyContentSpScDetailText1.setId(rs.getLong("id"));
                applyContentSpScDetailText1.setImageUrl(rs.getString("image_url"));
                applyContentSpScDetailText1.setLocation(rs.getInt("location"));
                applyContentSpScDetailText1.setFontType(rs.getInt("font_type"));
                applyContentSpScDetailText1.setFontSize(rs.getInt("font_size"));
                applyContentSpScDetailText1.setFontColor(rs.getString("font_color"));
                applyContentSpScDetailText1.setTextContent(rs.getString("text_content"));
                applyContentSpScDetailText1.setApplyContentSpScDetailId(rs.getLong("apply_content_sp_sc_detail_id"));
                applyContentSpScDetailTextList.add(applyContentSpScDetailText1);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn,stmt, rs);
        }
        return applyContentSpScDetailTextList;
    }
}
